# author: Jochen Rehmert, University of Basel
# journal: Journal of Politics
# article: Intra-party competition, geographic responsiveness 
#           and incumbent deselection in closed-list PR
# date: 12th August 2024
# content: Script to replicate Figure 12 in the Appendix
# needs data: "district_greens.RDS"

# directory
setwd("")

sink("log_rep_figure12.txt")
cat("start logging\n")

dat <- readRDS("district_greens.RDS")


cat("create Figure 12\n")
br = 50
lim.y <- c(0,30)
par(mfrow = c(3,3),
    oma = c(3,5,1,0) + 0.1,
    mar = c(3,0,1,1) + 0.1)
hist(dat$margin[dat$year == 1983], breaks = br, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "1983", xaxt = "n", yaxt = "n", col = "white")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")
axis(2, seq(0,40,10), seq(0,40,10), col.ticks = "black", col = "white")

hist(dat$margin[dat$year == 1987], breaks = br, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "1987", xaxt = "n", yaxt = "n", col = "white", ylab = "")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")

hist(dat$margin[dat$year == 1990], breaks = br, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "1990", xaxt = "n", yaxt = "n", col = "white", ylab = "")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")

hist(dat$margin[dat$year == 1994], breaks = br, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "1994", xaxt = "n", yaxt = "n", col = "white")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")
axis(2, seq(0,40,10), seq(0,40,10), col.ticks = "black", col = "white")

hist(dat$margin[dat$year == 1998], breaks = br, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "1998", xaxt = "n", yaxt = "n", col = "white", ylab = "")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")

hist(dat$margin[dat$year == 2002], breaks = br+10, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "2002", xaxt = "n", yaxt = "n", col = "white", ylab = "")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")

hist(dat$margin[dat$year == 2005], breaks = br, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "2005", xaxt = "n", yaxt = "n", col = "white")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")
axis(2, seq(0,40,10), seq(0,40,10), col.ticks = "black", col = "white")

hist(dat$margin[dat$year == 2009], breaks = br, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "2009", xaxt = "n", yaxt = "n", col = "white", ylab = "")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")

hist(dat$margin[dat$year == 2013], breaks = br, xlim = c(-70,5), ylim = lim.y, xlab = "", main = "2013", xaxt = "n", yaxt = "n", col = "white", ylab = "")
axis(1, seq(-70,0,10), seq(-70,0,10), col.ticks = "black", col = "white")

title(ylab = "Frequency", outer = TRUE, cex.lab = 1.5)
cat("end logging")
sink()